package com.gitee.wsl.compose.chart.xy.line.stack

import androidx.compose.foundation.layout.absolutePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gitee.wsl.common.ui.draw.style.LineStyle
import com.gitee.wsl.compose.chart.Chart
import com.gitee.wsl.compose.chart.ChartLayout
import com.gitee.wsl.compose.chart.ExperimentalKoalaPlotApi
import com.gitee.wsl.compose.chart.KoalaPlotTheme
import com.gitee.wsl.compose.chart.legend.Legend
import com.gitee.wsl.compose.chart.legend.LegendLocation
import com.gitee.wsl.compose.chart.legend.LegendSlate
import com.gitee.wsl.compose.chart.legend.defaultLegend
import com.gitee.wsl.compose.chart.xy.XYGraph
import com.gitee.wsl.compose.chart.xy.axis.AxisLabel
import com.gitee.wsl.compose.chart.xy.axis.AxisModel
import com.gitee.wsl.compose.chart.xy.axis.style.AxisStyle
import com.gitee.wsl.compose.chart.xy.axis.style.rememberAxisStyle
import com.gitee.wsl.compose.chart.xy.line.LineStackedPlot
import com.gitee.wsl.compose.chart.xy.line.LineStackedPlotSlate
import com.gitee.wsl.compose.chart.xy.line.area.model.AreaBaseline
import com.gitee.wsl.compose.chart.xy.model.ListPointEntry
import com.gitee.wsl.compose.modifier.input.gestures.GestureConfig


@OptIn(ExperimentalKoalaPlotApi::class)
@Composable
fun <X,Y> Chart.StackedArea(
    modifier: Modifier = Modifier,
    title: @Composable () -> Unit = {},
    legend: LegendSlate = Legend.defaultLegend,
    legendLocation: LegendLocation = KoalaPlotTheme.legendLocation,

    xAxisModel: AxisModel<X>,
    yAxisModel: AxisModel<Y>,
    xAxisStyle: AxisStyle = rememberAxisStyle(),
    xAxisLabels: @Composable (X) -> Unit = {
        AxisLabel(it.toString(), Modifier.padding(top = 2.dp))
    },
    xAxisTitle: @Composable () -> Unit = {},
    yAxisStyle: AxisStyle = rememberAxisStyle(),
    yAxisLabels: @Composable (Y) -> Unit ={
        AxisLabel(it.toString(), Modifier.absolutePadding(right = 2.dp))
    },
    yAxisTitle: @Composable () -> Unit = {},
    horizontalMajorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.majorGridlineStyle,
    horizontalMinorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.minorGridlineStyle,
    verticalMajorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.majorGridlineStyle,
    verticalMinorGridLineStyle: LineStyle? = KoalaPlotTheme.axisThemeStyle.minorGridlineStyle,
    gestureConfig: GestureConfig = GestureConfig(),
    datas: List<List<ListPointEntry<X, Y>>>,
    styles: (Int) -> List<StackedAreaStyle>,
    firstBaseline: (Int) -> AreaBaseline.ConstantLine<X, Y>,
    linePlotSlate: LineStackedPlotSlate<X, Y> = LineStackedPlot.StackedArea( styles = styles,firstBaseline = firstBaseline )
){
    ChartLayout(
        modifier = modifier,
        title = title,
        legend = legend,
        legendLocation = legendLocation
    ) {
        XYGraph(
            xAxisModel = xAxisModel,
            yAxisModel = yAxisModel,
            xAxisStyle = xAxisStyle,
            xAxisLabels = xAxisLabels,
            xAxisTitle = xAxisTitle,
            yAxisStyle = yAxisStyle,
            yAxisLabels = yAxisLabels,
            yAxisTitle = yAxisTitle,
            horizontalMajorGridLineStyle = horizontalMajorGridLineStyle,
            horizontalMinorGridLineStyle = horizontalMinorGridLineStyle,
            verticalMajorGridLineStyle =verticalMajorGridLineStyle,
            verticalMinorGridLineStyle = verticalMinorGridLineStyle,
            gestureConfig = gestureConfig,
        ){
            datas.forEachIndexed {i,it->
                linePlotSlate(i,it)
            }
        }
    }
}